﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SwParaPlatform2
{
    public partial class MainAssemDataGridView : Form
    {
        private Dictionary<string, Dictionary<string, object>> data;
        private DataGridView dataGridView;
        private Button saveButton;

        public MainAssemDataGridView(Dictionary<string, object> inputData)
        {
            InitializeComponent();
            data = ConvertInputData(inputData);
            InitializeDataGridView();
            LoadData();
        }

        private void InitializeComponent()
        {
            this.dataGridView = new DataGridView();
            this.saveButton = new Button();

            // 
            // dataGridView
            // 
            this.dataGridView.AllowUserToAddRows = false;
            this.dataGridView.AllowUserToDeleteRows = false;
            this.dataGridView.AllowUserToResizeColumns = false;
            this.dataGridView.AllowUserToResizeRows = false;
            this.dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridView.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;
            this.dataGridView.Dock = DockStyle.Top;  // 设置 DataGridView 填充顶部
            this.dataGridView.Height = 600; // 设置固定高度
            this.dataGridView.CellFormatting += new DataGridViewCellFormattingEventHandler(this.DataGridView_CellFormatting);

            // 
            // saveButton
            // 
            this.saveButton.Dock = DockStyle.Bottom;
            this.saveButton.Name = "saveButton";
            this.saveButton.Size = new System.Drawing.Size(100, 20);
            this.saveButton.TabIndex = 1;
            this.saveButton.Text = "Save";
            this.saveButton.UseVisualStyleBackColor = true;
            this.saveButton.Click += new EventHandler(SaveButton_Click);

            // 
            // Form1
            // 
            this.ClientSize = new System.Drawing.Size(800, 600);
            this.Controls.Add(this.saveButton);
            this.Controls.Add(this.dataGridView);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.Name = "Form1";
            this.Text = "Dictionary to DataGridView";
        }

        private Dictionary<string, Dictionary<string, object>> ConvertInputData(Dictionary<string, object> inputData)
        {
            var result = new Dictionary<string, Dictionary<string, object>>();

            foreach (var kvp in inputData)
            {
                if (kvp.Value is Dictionary<string, object> subDict)
                {
                    result[kvp.Key] = subDict;
                }
                else
                {
                    throw new InvalidCastException($"Value for key '{kvp.Key}' is not a Dictionary<string, object>.");
                }
            }

            return result;
        }

        private void InitializeDataGridView()
        {
            dataGridView.Columns.Add("Name", "Name");

            if (data.Count > 0)
            {
                foreach (var key in data.First().Value.Keys)
                {
                    dataGridView.Columns.Add(key, key);
                }
            }

            dataGridView.Columns["Name"].ReadOnly = true;

            // 设置 "Equation" 列宽度固定为 100
            if (dataGridView.Columns.Contains("Equation"))
            {
                dataGridView.Columns["Equation"].Width = 300;
                dataGridView.Columns["Equation"].AutoSizeMode = DataGridViewAutoSizeColumnMode.None;
            }
        }

        private void LoadData()
        {
            foreach (var item in data)
            {
                var row = new DataGridViewRow();
                row.CreateCells(dataGridView);
                row.Cells[0].Value = item.Key;

                int cellIndex = 1;
                foreach (var subItem in item.Value)
                {
                    row.Cells[cellIndex].Value = subItem.Value;
                    if (subItem.Key != "Equation")
                    {
                        row.Cells[cellIndex].ReadOnly = true;
                    }
                    cellIndex++;
                }

                dataGridView.Rows.Add(row);
            }
        }

        private void DataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            // 如果是标题栏或第一列，跳过
            if (e.RowIndex == -1 || e.ColumnIndex == 0)
                return;

            // 检查单元格是否可编辑
            if (dataGridView.Columns[e.ColumnIndex].Name != "Equation" && dataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly)
            {
                e.CellStyle.BackColor = Color.GhostWhite;
                e.CellStyle.ForeColor = Color.DarkGray;
            }
        }

        private void SaveButton_Click(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView.Rows)
            {
                string name = row.Cells[0].Value.ToString();

                for (int i = 1; i < dataGridView.Columns.Count; i++)
                {
                    string key = dataGridView.Columns[i].Name;
                    object value = row.Cells[i].Value;

                    data[name][key] = value;
                }
            }

            MessageBox.Show("Data saved successfully!");
        }
    }
}
